<template>
  <el-button type="danger" :icon="Plus" @click="onSubmit()">新增</el-button>
  <el-form :inline="true" :model="adddata" class="demo-form-inline">
    <el-form-item label="部门名称">
      <el-input
        v-model="adddata.ablactationPro"
        placeholder="请输入部门名称"
        clearable
      />
    </el-form-item>
    <el-form-item>
      <el-button type="danger" @click="gettableData()">搜索</el-button>
    </el-form-item>
  </el-form>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column prop="ablactationId" label="编号" />
    <el-table-column prop="basicInforId" label="耳号" />
    <el-table-column prop="bedroomId" label="当前栋舍" />
    <el-table-column prop="fieldId" label="当前栏位" />
    <el-table-column prop="ablactationEseat" label="转入栏位" />
    <el-table-column prop="ablactationNum" label="断奶只数" />
    <el-table-column prop="ablactationSum" label="断奶总窝重(公斤)" />
    <el-table-column prop="ablactationWeight" label="种母重量(公斤)" />

    <el-table-column prop="" label="操作">
      <template #default="scope">
        <!-- <el-button type="primary" @click="handleEdit(scope.row.ablactationId)">删除</el-button> -->
        <el-button type="primary" @click="handleEdit()">删除</el-button>
        <el-button type="danger" @click="handleDelete(scope.row.ablactationId)"
          >修改</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页 -->
  <div>
    <el-pagination
      v-model:current-page="adddata.pageIndex"
      v-model:page-size="adddata.pageSize"
      :page-sizes="[3, 5, 8, 10]"
      :background="true"
      layout="total, sizes, prev, pager, next, jumper"
      :total="adddata.totalCount"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>
   <script lang="ts" setup>
import axios from "axios";
import { ref, reactive, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRoute, useRouter } from "vue-router";
import { Delete, Edit, Search, Share, Upload } from "@element-plus/icons-vue";
import type { ComponentSize, FormInstance, FormRules } from "element-plus";
import { Plus } from "@element-plus/icons-vue";
import type { UploadProps, UploadUserFile } from "element-plus";

const apiUrl = import.meta.env.VITE_APP_API_URL;
const apiBedinUrl = import.meta.env.VITE_APPRead_API_URL;

const route = useRoute();
const router = useRouter();
const adddata = reactive({
  ablactationId: "",
  ablactationPro: "",
  pageIndex: 1,
  pageSize: 3,
  totalCount: 0,
  pageCount: 0,
});

const tableData = ref([]);
const gettableData = () => {
  axios
    .post(apiUrl+"/api/Ablactations/showAblactations", {
      ablactationPro: adddata.ablactationPro,
      pageIndex: adddata.pageIndex,
      pageSize: adddata.pageSize,
    })
    .then((res) => {
      tableData.value = res.data.data.list;
      adddata.pageCount = res.data.data.pageCount;
      adddata.totalCount = res.data.data.totalCount;
    });
};
//分页
const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  adddata.pageSize = val;
  gettableData();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  adddata.pageIndex = val;
  gettableData();
};
const onSubmit = () => {
  router.push({ name: "AddAblactation" });
};

const handleEdit = () => {
  ElMessageBox.confirm("确认要删除吗?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      ElMessage({
        type: "success",
        message: "删除成功",
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消删除",
      });
    });
};

// //删除
// const handleEdit = (ablactationId: any) => {
//   ElMessageBox.confirm(
//     '确认要删除吗?',
//     'Warning',
//     {
//       confirmButtonText: '确认',
//       cancelButtonText: '取消',
//       type: 'warning',
//     }
//   )
//     .then(() => {
//       axios.put(apiUrl+'/api/Ablactations/DeleteAblactation', {
//         ablactationId: ablactationId
//       })
//         .then(res => {
//           if (res.data.code == 0) {
//             ElMessage.success('删除成功');
//             gettableData()
//           }
//           else {
//             ElMessage.error('删除失败');
//             return;
//           }
//         })
//     })
//     .catch(() => {
//       ElMessage({
//         type: 'info',
//         message: '取消删除',
//       })
//     })
// }
//查看母猪断奶修改
const handleDelete = (ablactationId: number) => {
  router.push({
    path: "/",
    query: {
      ablactationId: ablactationId,
    },
  });
};
onMounted(() => {
  gettableData();
});
</script>
